﻿#pragma once


namespace cx
{
	template<class T>
	struct ListNode
	{
		T _data;
		ListNode<T>* _prev;
		ListNode<T>* _next;

		ListNode(const T& data = T())
			:_data(data)
			, _prev(nullptr)
			, _next(nullptr)
		{}
	};


	template<class T,class Ref,class Ptr>
	struct ListIterator
	{
		typedef ListNode<T> Node;
		typedef ListIterator<T,Ref,Ptr> Self;
		Node* _node;
		ListIterator(Node* node)
			:_node(node)
		{}

		Self& operator++()
		{
			_node = _node->_next;
			return *this;
		}

		Self& operator--()
		{
			_node = _node->_prev;
			return *this;
		}


		Self operator++(int)
		{
			Self tmp(*this);
			_node = _node->_next;
			return tmp;
		}

		Self& operator--(int)
		{
			Self tmp(*this);
			_node = _node->_prev;
			return tmp;
		}

		Ref operator*() 
		{
			return _node->_data;
		}

		Ptr operator->()
		{
			return &_node->_data;
		}



		
		bool operator!=(const Self& it)
		{
			return _node != it._node;
		}

		bool operator==(const Self& it)
		{
			return _node == it._node;
		}


	};

	
	// 适配器 -- 复用
	template<class T, class Ref, class Ptr>
	struct Reverse_Listiterator
	{
		typedef ListNode<T> Node;
		typedef Reverse_Listiterator<T, Ref, Ptr> Self;

		Node* _node;

		Reverse_Listiterator(Node* node)
			:_node(node)
		{}

		Self& operator++()
		{
			_node = _node->_prev;
			return *this;
		}

		Self& operator--()
		{
			_node = _node->_next;
			return *this;
		}

		Self operator++(int)
		{
			Self tmp(*this);
			_node = _node->_prev;
			return tmp;
		}

		Self operator--(int)
		{
			Self tmp(*this);
			_node = _node->_next;
			return tmp;
		}
		
		Ref operator*()
		{
			return _node->_data;
		}

		Ptr operator->()
		{
			return &_node->_data;
		}


		bool operator!=(const Self& it)
		{
			return _node != it._node;
		}

		bool operator==(const Self& it)
		{
			return _node == it._node;
		}

	};
	


	template<class T>
	class list
	{
	public:
		typedef ListNode<T> Node;
		typedef ListIterator<T, T&, T*> iterator;
		typedef ListIterator<T, const T&, const T*> const_iterator;
		typedef Reverse_Listiterator<T, const T&, const T*> const_reverse_iterator;
		typedef Reverse_Listiterator<T, T&, T*> reverse_iterator;

		iterator begin()
		{
			return iterator(_head->_next);
		}

		iterator end()
		{
			return iterator(_head);
		}

		const_iterator begin() const
		{
			return const_iterator(_head->_next);
		}

		const_iterator end() const
		{
			return const_iterator(_head);
		}

		reverse_iterator rbegin()
		{
			return reverse_iterator(_head->_prev);
		}

		reverse_iterator rend()
		{
			return reverse_iterator(_head);
		}

		const_reverse_iterator rbegin() const
		{
			return const_reverse_iterator(_head->_prev);
		}

		const_reverse_iterator rend() const
		{
			return const_reverse_iterator(_head);
		}

		void empty_init()
		{
			_head = new Node;
			_head->_prev = _head;
			_head->_next = _head;
		}

		list()
		{
			empty_init();
		}

		list(initializer_list<T> il)
		{
			empty_init();

			for (const auto& e : il)
			{
				push_back(e);
			}

		}

		list(const list<T>& lt)
		{
			empty_init();

			for (const auto& e : lt)
			{
				push_back(e);
			}
		}

		template <class Iterator>
		list(Iterator first, Iterator last)
		{
			empty_init();

			while (first != last)
			{
				push_back(*first);
				first++;
			}
		}

		list<T>& operator=(list<T> lt)
		{
			swap(_head, lt._head);

			return *this;
		}

		~list()
		{
			clear();
			
			delete _head;
			_head = nullptr;
		}

		void clear()
		{
			list<T>::iterator it = begin();
			while (it != end())
			{
				it = erase(it);
			}
		}

		Node* BuyNode(const T& x)
		{
			Node* newnode = new Node;
			newnode->_prev = newnode;
			newnode->_next = newnode;
			newnode->_data = x;
			return newnode;
		}

		void push_back(const T& x)
		{
			/*Node* newnode = BuyNode(x);
			Node* tail = _head->_prev;

			newnode->_prev = tail;
			newnode->_next = _head;
			_head->_prev = newnode;
			tail->_next = newnode;*/
			insert(end(), x);
		}

		void pop_back()
		{
			/*assert(begin() != end());
			Node* tail = _head->_prev;
			tail->_prev->_next = _head;
			_head->_prev = tail->_prev;

			delete tail;*/
			erase(--end());
		}

		void push_front(const T& x)
		{
			insert(begin(), x);
		}

		void pop_front()
		{
			erase(begin());
		}

		iterator insert(iterator pos,const T& x)
		{
			Node* cur = pos._node;
			Node* prev = cur->_prev;
			Node* newnode = BuyNode(x);

			prev->_next = newnode;
			newnode->_prev = prev;
			newnode->_next = cur;
			cur->_prev = newnode;
			return iterator(cur->_prev);
		}

		iterator erase(iterator pos)
		{
			assert(pos != end());
			Node* cur = pos._node;
			Node* prev = cur->_prev;
			Node* next = cur->_next;

			prev->_next = next;
			next->_prev = prev;

			delete cur;

			return iterator(next);
		}


		void reserve()
		{
			if (begin() == --end())
			{
				return;
			}


			Node* prev = _head;
			Node* cur = _head->_next;
			Node* next = cur->_next;

			Node* key = cur;

			do
			{
				cur->_prev = next;
				cur->_next = prev;

				prev = cur;
				cur = next;
				next = next->_next;
			}while (cur != key);
		}

		size_t size()const
		{
			size_t size = 0;
			Node* cur = _head->_next;
			while (cur != _head)
			{
				cur = cur->_next;
				size++;
			}
			return size;
		}

		bool empty()const
		{
			return begin() == end();
		}

		T& front()
		{
			return _head->_next->_data;
		}

		const T& front()const
		{
			return _head->_next->_data;
		}

		T& back()
		{
			return _head->_prev->_data;
		}

		const T& back()const
		{
			return _head->_prev->_data;
		}

		void swap(list<T>& l)
		{
			std::swap(_head, l._head);
		}


	

	private:
		Node* _head;
	};
}